<template>
  <lb-table
    ref="lbTable"
    v-el-height-adaptive-table="{bottomOffset:10}"
    border
    element-loading-text="加载中"
    :column="tableData.column"
    :data="tableData.data"
    :pagination="false"
    @fetch="fetchData"
  />
</template>

<script>
  import { queryMap, queryMessageTemplateList, saveMessageTemplate } from '@/api'
  import { mergeAsyncFunc } from '@/utils/mergeAsyncFunc'
  import { formatColumnMap } from '@/utils'

  export default {
    name: 'message-template',
    data() {
      return {
        msgTypeMap: [],
        tableData: {
          column: [
            {
              prop: 'msgType',
              width: 150,
              label: '业务类型',
              render: (h, { row }) => <span>{ row.msgType && formatColumnMap(this.msgTypeMap, 'value', row.msgType, 'desc') }</span>
            },
            {
              prop: 'templateContent',
              label: '消息内容'
            },
            {
              prop: 'wechatIsOpen',
              width: 150,
              label: '微信推送',
              render: (h, { row }) => {
                const dom = <el-switch
                  onChange={e => this.changeSwitch(e, row)}
                  v-model={row.switch}
                  activeText='是'
                  inactiveText='否'
                >
                </el-switch>
                return dom
              }
            }
          ],
          data: []
        }
      }
    },
    created() {
      this.queryOptionsMap()
    },
    methods: {
      async fetchData() {
        const res = await queryMessageTemplateList()
        this.tableData.data = res.data.map(item => {
          return {
            ...item,
            switch: item.wechatIsOpen === 2
          }
        })
      },
      async changeSwitch(value, row) {
        console.log(value, row)
        const params = {
          ...row,
          wechatIsOpen: value ? 2 : 1
        }
        await saveMessageTemplate(params)
        this.$refs.lbTable.fetch()
        this.$notify({
          title: '提示',
          message: '保存成功',
          type: 'success',
          duration: 2000
        })
      },
      // 查询枚举
      async queryOptionsMap() {
        const res = await mergeAsyncFunc(queryMap, { enumName: 'MessageTemplateType' })
        this.msgTypeMap = res.data // 业务类型
      },
      search() {
        this.$refs.lbTable.fetch()
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>
